package com.teacher.service.impl;

import com.teacher.entity.EvaluationCriterion;
import com.teacher.repository.EvaluationCriterionRepository;
import com.teacher.service.EvaluationCriterionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class EvaluationCriterionServiceImp implements EvaluationCriterionService {
    @Autowired
    EvaluationCriterionRepository evaluationCriterionRepository;

    /**
     * 增加
     * @param evaluationCriterion
     * @return
     */
    @Override
    public EvaluationCriterion addEvaluationCriterion(EvaluationCriterion evaluationCriterion) {
        EvaluationCriterion result = new EvaluationCriterion();
        try {
            if(evaluationCriterionRepository.existsByDimensionCodeAndClassifyCode(
                    evaluationCriterion.getDimensionCode(),evaluationCriterion.getClassifyCode())){
                throw new RuntimeException("评价要素已存在！");
            }
            evaluationCriterion.setCreateDate(new Date());
            evaluationCriterion.setUpdateDate(new Date());
            result = evaluationCriterionRepository.save(evaluationCriterion);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 修改
     * @param evaluationCriterion
     * @return
     */
    @Override
    public EvaluationCriterion updateEvaluationCriterion(EvaluationCriterion evaluationCriterion) {
        evaluationCriterion.setUpdateDate(new Date());
        return evaluationCriterionRepository.saveAndFlush(evaluationCriterion);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public EvaluationCriterion getDetailsById(String id) {
        return evaluationCriterionRepository.findById(Integer.valueOf(id)).get();
    }


    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteEvaluationCriterion(String id) {
        EvaluationCriterion evaluationCriterion = null;
        try{
            evaluationCriterion = evaluationCriterionRepository.findById(Integer.valueOf(id)).get();
            evaluationCriterionRepository.delete(evaluationCriterion);
        }catch (RuntimeException e){
            e.printStackTrace();
        }
    }

    /**
     * 批量删除
     * @param idList
     */
    @Override
    public void deleteEvaluationCriterionList(String idList) {
        String[] split = idList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        evaluationCriterionRepository.deleteEvaluationCriterionByIdIn(list);
    }

    /**
     * 查询所有
     */

    @Override
    public List<EvaluationCriterion> findAll() {
        return evaluationCriterionRepository.findAll();
    }

    /**
     * 根据条件分页查询
     * @param evaluationCriterion,page,size
     */
    @Override
    public Page<EvaluationCriterion> specificPageQueryEvaluationCriterionList(EvaluationCriterion evaluationCriterion, int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(evaluationCriterion != null)
                {
                    // 评价维度
                    Integer dimensionCode = evaluationCriterion.getDimensionCode();
                    if (dimensionCode != null) {
                        predicates.add(criteriaBuilder.equal(root.get("dimensionCode").as(Integer.class), dimensionCode));
                    }
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return evaluationCriterionRepository.findAll(querySpecification,PageRequest.of(page-1,size));
    }
}
